package cn.edu.scau.cmi.chenxiaoqin.client.total;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import cn.edu.scau.cmi.chenxiaoqin.composite.safe.SafeOrganization;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextField;
import javafx.scene.control.TextInputDialog;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class SafeCompositeClient extends Application {
	List<SafeOrganization> safeOrganizations = new ArrayList<SafeOrganization>();
	TreeItem<String> rootNode = new TreeItem<>("组织");
	
	public void start(Stage stage) {
		rootNode.setExpanded(true);
		stage.setTitle("安全组合模式");
		 VBox box = new VBox();
		 Scene scene = new Scene(box, 400, 300);
		 scene.setFill(Color.LIGHTGRAY);
		 TreeView<String> treeView = new TreeView<>(rootNode);
		 treeView.setEditable(true);
         treeView.setCellFactory((TreeView<String> p) -> 
             new TextFieldTreeCellImpl());
         box.getChildren().add(treeView);
         stage.setScene(scene);
         stage.show();
	}
	private final class TextFieldTreeCellImpl extends TreeCell<String> {
		 private TextField textField;
         private final ContextMenu addMenu = new ContextMenu();
         private final ContextMenu deleteMenu = new ContextMenu();
         public TextFieldTreeCellImpl() {
        	 MenuItem addTeamMenuItem = new MenuItem("添加团队");
        	 MenuItem addPersonMenuItem = new MenuItem("添加个人");
             MenuItem deleteMenuItem = new MenuItem("删除团队");
             MenuItem deleteMenuItem0 = new MenuItem("删除个人");
             deleteMenu.getItems().add(deleteMenuItem0 );
             addMenu.getItems().addAll(addTeamMenuItem ,addPersonMenuItem,deleteMenuItem);
             addTeamMenuItem.setOnAction((ActionEvent t) -> {
            	 TextInputDialog dialog = new TextInputDialog();
            	 dialog.setTitle("团体添加界面");
                 dialog.setContentText("请输入团体名:");
                 Optional<String> result = dialog.showAndWait();
                 String teamString=null;
                 if (result.isPresent()) {                  
                     teamString = result.get();
                     TreeItem newOrganization = 
                             new TreeItem<>(teamString);
                     	newOrganization.setExpanded(true);
                         getTreeItem().getChildren().add(newOrganization);              
                  }
             });
             addPersonMenuItem.setOnAction((ActionEvent t) -> {
            	 TextInputDialog dialog = new TextInputDialog();
                 dialog.setTitle("个人添加界面");
                 dialog.setContentText("请输入个人名:");
                 Optional<String> result = dialog.showAndWait();
                 //确认输入了内容，避免NPE
                 String teamString=null;
                 if (result.isPresent()) {                  
                     teamString = result.get();
                     TreeItem newOrganization = 
                             new TreeItem<>(teamString);
                     	newOrganization.setExpanded(false);
                         getTreeItem().getChildren().add(newOrganization);              
                  }               
             });
             deleteMenuItem.setOnAction((ActionEvent t) -> {       	
            	 getTreeItem().getParent().getChildren().remove(getTreeItem());  	 
              }); 
             deleteMenuItem0.setOnAction((ActionEvent t) -> {
             	getTreeItem().getParent().getChildren().remove(getTreeItem());
               });  
         }
         @Override
         public void startEdit() {
             super.startEdit();
  
             if (textField == null) {
                 createTextField();
             }
             setText(null);
             setGraphic(textField);
             textField.selectAll();
         }
  
         @Override
         public void cancelEdit() {
             super.cancelEdit();
  
             setText((String) getItem());
             setGraphic(getTreeItem().getGraphic());
         }
         @Override
         public void updateItem(String item, boolean empty) {
             super.updateItem(item, empty);
  
             if (empty) {
                 setText(null);
                 setGraphic(null);
             } else {
                 if (isEditing()) {
                     if (textField != null) {
                         textField.setText(getString());
                     }
                     setText(null);
                     setGraphic(textField);
                 } else {
                     setText(getString());
                     setGraphic(getTreeItem().getGraphic());
                    // System.out.println(getTreeItem().getValue());
                     if (
                     		getTreeItem().isExpanded()
                     ){
                         setContextMenu(addMenu);
                     }else 
                     if(!getTreeItem().isExpanded()) {
                    	 setContextMenu(deleteMenu);
                     }
                 }
             }
         }
         private void createTextField() {
             textField = new TextField(getString());
             textField.setOnKeyReleased((KeyEvent t) -> {
                 if (t.getCode() == KeyCode.ENTER) {
                     commitEdit(textField.getText());
                 } else if (t.getCode() == KeyCode.ESCAPE) {
                     cancelEdit();
                 }
             });  
         }
         private String getString() {
             return getItem() == null ? "" : getItem().toString();
         }
	}
}
